
package com.yuke.cloud.service.uac.mq.producer;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.yuke.cloud.common.base.constant.MQConstants;
import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.core.mq.RabbitMetaMessage;
import com.yuke.cloud.service.api.opc.dto.SendEmailRequest;
import com.yuke.cloud.service.api.uac.exceptions.UacBizException;
import com.yuke.cloud.service.uac.enums.UacEmailTemplateEnum;
import com.yuke.cloud.service.uac.service.UacFreeMarkerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

/**
 * The class Email producer.
 *
 * @author
 */
@Component
@Slf4j
public class EmailProducer {
	@Resource
	private UacFreeMarkerService uacFreeMarkerService;

	/**
	 * Send email mq.
	 *
	 * @param emailSet          the email set
	 * @param emailTemplateEnum the email template enum
	 * @param param             the param
	 */
	public RabbitMetaMessage sendEmailMq(Set<String> emailSet, UacEmailTemplateEnum emailTemplateEnum, Map<String, Object> param) {
		log.info("pcSendEmailRequest - 发送邮件MQ. emailSet={}, param={}", emailSet, param);
		String msgBody;
		try {

			Preconditions.checkArgument(emailTemplateEnum != null, "邮箱模板信息不存在");
			SendEmailRequest request = new SendEmailRequest();

			request.setMsgType(MQConstants.MQ_OPC_SEND_EMAIL);

			String templateLocation = emailTemplateEnum.getLocation();
			String text = uacFreeMarkerService.getTemplate(param, templateLocation);

			request.setText(text);
			request.setTo(emailSet);
			request.setSubject(emailTemplateEnum.getSubject());

			msgBody = JSON.toJSONString(request);
		} catch (Exception e) {
			log.error("发送邮件验证码 smsMessage转换为json字符串失败", e);
			throw new UacBizException(ErrorCodeEnum.UAC10011043);
		}

		RabbitMetaMessage  rabbitMetaMessage = new RabbitMetaMessage();
		rabbitMetaMessage.setExchange(MQConstants.BUSINESS_EXCHANGE);
		rabbitMetaMessage.setRoutingKey(MQConstants.BUSINESS_KEY_OPC);
		rabbitMetaMessage.setPayload(msgBody);
		return rabbitMetaMessage;
	}
}
